package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.StringUtil;
import com.item.dao.model.NewsImg;
import com.item.dao.model.NewsImgExample;
import com.item.dao.model.NewsImgExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.NewsImgService;
import com.item.util.WebPageFileController;

@Controller
public class NewsImgController {
	@Autowired
	private NewsImgService newsImgService;
	
	@RequestMapping("/listNewsImg")
	@ResponseBody
	public String list(Page param,String key, String source,String title, Integer isM) throws Exception{
		NewsImgExample example = new NewsImgExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		Criteria criteria = example.createCriteria();
		Criteria c2 = example.createCriteria();
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(source)){
			criteria.andSourceLike("%"+source+"%");
			c2.andSourceLike("%"+source+"%");
		}
		if(StringUtils.isNotBlank(title)){
			criteria.andTitleLike("%"+title+"%");
			c2.andTitleLike("%"+title+"%");
		}
		example.or(c2);
		example.setOrderByClause("time DESC");
		List<NewsImg> list = newsImgService.selectByExample(example);
		
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), isM);
	}
	
	@RequestMapping("/showlistNewsImg")
	public String list() {
		return "newsImg/newsImgList";
	}
	
	@RequestMapping("/toEditNewsImg")
	public String toEdit()throws Exception{
		return "newsImg/newsImgEdit";
	}
	
	@RequestMapping(value="/saveNewsImg",method=RequestMethod.POST)
	@ResponseBody
	public String save(String contextReal,NewsImg newsImg, Integer isTop, HttpSession session)throws Exception{
		WebPageFileController fileController = new WebPageFileController("bs", "newsImg", "utf8");
		newsImg.setTime(new Date());
		try{
			if(StringUtils.isNotBlank(newsImg.getAppid())) {
				fileController.setParams("bs", "newsImg", "utf8");
			}
			if(StringUtil.isEmpty(newsImg.getId())){
				if(StringUtils.isNotBlank(contextReal)) {
					newsImg.setTop(0);
					newsImg.setUrl(fileController.write(fileController.getHtmlContentUTF8(contextReal), "html"));
					
					newsImgService.insert(newsImg);
				}	
			}else{
				if(!StringUtils.isBlank(contextReal)) {
					//覆盖原来的文件
					newsImg.setUrl(fileController.writeByUrl(fileController.getHtmlContentUTF8(contextReal), newsImg.getUrl()));
					newsImgService.updateByPrimaryKeySelective(newsImg);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdNewsImg")
	@ResponseBody
	public String findById(String id,HttpSession session, Integer isM)throws Exception{		
		NewsImg n = newsImgService.selectByPrimaryKey(id);

		return JSONRepUtils.serialize(n, isM);
	}
	
	@RequestMapping("/delNewsImg")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
//		WebPageFileController fileController = new WebPageFileController();
		String[] ids = id.split(",");
//		String[] urls = url.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			System.out.println(ids[i]);
			newsImgService.deleteByPrimaryKey(ids[i]);
//			fileController.deleteFile(urls[i]);
		}
		return "{\"success\":true}";
	}

}

